<?php

class Application_Model_Users extends Application_Model_DbTable_Users {

    /**
     * Get login user 
     * @param string $username
     * @param string $password
     */
    public function login($username, $password) {
        $select = $this->select()->from('users');
        $select->where('username = ?', $username);
        $select->where('password = ?', $password);
        $user = $this->fetchRow($select);
        if ($user) {
            return true;
        } else {
            return false;
        }
    }
    
    /**
     * Change password
     * @author Tien Nguyen
     * @param type $username
     * @param type $newPassword 
     */
    public function changePassword($username, $newPassword){
        $select = $this->select()
                ->from('users')
                ->where('username = ?', $username);
        $user = $this->fetchRow($select);
        
        if($username){
            
            $data = array('password' => $newPassword);
            $user->setFromArray($data);
            return $user->save();
        }
        return false;
    }
    
    /**
     * Change profile for user
     * @author Tien Nguyen
     * @param string $username
     * @param string $password
     * @param object $data 
     * @return boolean
     */
    public function changeProfile($username, $password, $data){
        $newData = array();
        $data->country_id = $data->countryId;
        Webservices_Array_Format::convertObjectToArray($data, $newData);
        return $this->edit($newData);
    }

    /**
     * Find user name by user name
     * @param string $username
     * @param string $password
     * @param string $name
     * @return array
     */
    public function findUser($name){
         $select = $this->select()
         ->from('Users')
         ->setIntegrityCheck(FALSE)
         ->where('username like ?', '%'.$name.'%');
         $res = $this->fetchAll($select);
         if($res){
            $res = $res->toArray();
         }
         return $res;
    }
    
    /**
     * Get friend by username
     * @author Tien Nguyen
     * @param string $username
     * @return Zend_Db_Table_Rowsey_Abstract 
     */
    public function getFriends($username){
        $modelDbUserFriends = new Application_Model_DbTable_UserFriends();
        $userFriends = $modelDbUserFriends->getFriends($username);
        return $userFriends;
    }
    
    /**
     * Convert Object to array
     * @param object $obj
     */
    public function convertObjectToArray($obj){
        $arr = array();
        foreach($obj as $key => $val){
            $arr[$key] = $val;
        }
        return $arr;
    }
    
    /**
     * get user by user name
     */
    public function getUserByUsername($username){
        $select = $this->select()
        ->from('users', array('user_id', 'username', 'distance', 'age', 'fullname', 
            'country_id', 'img'))
        ->where('users.username = ?', $username);
        $row = $this->fetchRow($select);
        return $row;
    }
    
    public function convertField2(&$arr){
        $arr['username'] = $arr['username'];
        $arr['country_id'] = $arr['countryId'];
        $arr['user_id'] = $arr['userId'];
    }
    
    public function convertField(&$arr){
        $arr['username'] = $arr['username'];
        $arr['countryId'] = $arr['country_id'];
        $arr['userId'] = $arr['user_id'];
    }
}